using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_SettingGame : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_SettingGame
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ToggleGroup toggleStoryDefaultName { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleStoryAutoSpeed { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleFrame { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleBigSkillAnimation { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleStoryPlayAuto { protected set; get; }
            public UnityEngine.UI.ToggleGroup toggleQuality { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleStoryDefaultName", out var __tbv0);
                this.toggleStoryDefaultName = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleStoryAutoSpeed", out var __tbv1);
                this.toggleStoryAutoSpeed = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleFrame", out var __tbv2);
                this.toggleFrame = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleBigSkillAnimation", out var __tbv3);
                this.toggleBigSkillAnimation = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleStoryPlayAuto", out var __tbv4);
                this.toggleStoryPlayAuto = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.UI.ToggleGroup>("toggleQuality", out var __tbv5);
                this.toggleQuality = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private SettingGameData data;
        #endregion fields

        #region properties

        protected UIB_UIN_SettingGame ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_SettingGame();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        public void Show(int index)
        {
            this.data = GameDataCenter.PlayerData.setting.gameData;
            this.Show();
            this.ShowQuality();
            this.ShowFrame();
            this.ShowBigSkillAnimation();
            this.ShowStoryDefaultName();
            this.ShowStoryAutoPlay();
            this.ShowStoryAutoSpeed();
        }

        private void ShowQuality()
        {
            ShowToggle(this.ui.toggleQuality, (int)this.data.quality);
        }

        private void ShowToggle(UnityEngine.UI.ToggleGroup group, int index)
        {
            var child = group.transform.GetChild(index);
            var toggle = child.GetComponent<ExToggle>();
            toggle.isOn = true;
            // group.NotifyToggleOn(toggle);
        }

        private void ShowFrame()
        {
            ShowToggle(this.ui.toggleFrame, (int)this.data.highFrame);
        }

        private void ShowBigSkillAnimation()
        {
            ShowToggle(this.ui.toggleBigSkillAnimation, (int)this.data.bigSkillPlayModel);
        }

        private void ShowStoryDefaultName()
        {
            ShowToggle(this.ui.toggleStoryDefaultName, (int)this.data.storyDefaultName);
        }

        private void ShowStoryAutoPlay()
        {
            ShowToggle(this.ui.toggleStoryPlayAuto, (int)this.data.storyAutoPlay);
        }

        private void ShowStoryAutoSpeed()
        {
            ShowToggle(this.ui.toggleStoryAutoSpeed, (int)this.data.storyPlaySpeed);
        }

        public void Save()
        {
            int quality = GetIndex(this.ui.toggleQuality);
            int frame = GetIndex(this.ui.toggleFrame);
            int bigSkill = GetIndex(this.ui.toggleBigSkillAnimation);
            int storyDefaultName = GetIndex(this.ui.toggleStoryDefaultName);
            int storyAutoPlay = GetIndex(this.ui.toggleStoryPlayAuto);
            int storyAutoSpeed = GetIndex(this.ui.toggleStoryAutoSpeed);
            this.data.Set(Tuple.Create(quality, frame, bigSkill, storyDefaultName, storyAutoPlay, storyAutoSpeed));

            GameDataCenter.PlayerData.setting.SaveGameData(this.data);
        }

        private int GetIndex(UnityEngine.UI.ToggleGroup group)
        {
            var toggleOn = group.GetFirstActiveToggle();
            for (int i = 0; group.transform.childCount > 0; i++)
            {
                if (toggleOn.transform == group.transform.GetChild(i))
                    return i;
            }
            return -1;
        }

        #endregion methonds
    }
}
